package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.SetmealDto;
import com.itheima.entity.Category;
import com.itheima.entity.Setmeal;
import com.itheima.service.CategoryService;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Auther: guozy
 * @Date: 2022/8/1 - 08 - 01 - 11:54
 * @Description: com.itheima.controller
 * @version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 保存套餐表Setmeal,和套餐菜品关系表SetmealDish
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("传入{}保存套餐表和套餐菜品关系表",setmealDto);
        // 保存套餐
        setmealService.saveWithSetmealDish(setmealDto);

        return R.success("保存成功");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<SetmealDto>> page(int page,int pageSize,String name){
        log.info("分页查询page={},pageSize={},name={}",page,pageSize,name);

        //分页构造器
        Page<Setmeal> setmealPage = new Page<>(page,pageSize);

        //条件过滤
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();

        //排序条件
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        //查询分页
        setmealService.page(setmealPage,queryWrapper);

        Page<SetmealDto> setmealDtoPage = new Page<>();

        BeanUtils.copyProperties(setmealPage, setmealDtoPage, "records");

        //用categoryId,取出套餐分类名字,并拿List<SetmealDto>集合收集
        List<SetmealDto> setmealDtoList = setmealPage.getRecords().stream().map(item -> {
            SetmealDto setmealDto = new SetmealDto();

            BeanUtils.copyProperties(item, setmealDto);

            //通过分类categoryId取到其名字
            Long categoryId = item.getCategoryId();

                //通过setmealDishService调用getById方法,将categoryId转为套餐名字
                Category category = categoryService.getById(categoryId);

                //健壮性判断
                if (category!=null){

                    //取出套餐名字
                    String categoryName = category.getName();

                    //赋值
                    setmealDto.setCategoryName(categoryName);
                }

            return setmealDto;
        }).collect(Collectors.toList());

        //将过滤的属性重新赋值
        setmealDtoPage.setRecords(setmealDtoList);
        //设置
        return R.success(setmealDtoPage);
    }

    /**
     * 根据ids集合删除setmeal表和setmeal_dish表内容
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delect(@RequestParam List<Long> ids){

        setmealService.removeWithSetmealDish(ids);
        return R.success("删除成功");
    }

    /**
     * 根据条件查询数据
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> getList(Setmeal setmeal){
        log.info("根据{}查询菜品",setmeal);
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }



}

